#!/usr/bin/env python
# -*- coding: utf-8 -*-

import requests
import schedule
import ssl
import time
import random
import datetime
from  bs4 import  BeautifulSoup
import os
import re

requests.packages.urllib3.disable_warnings()
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
context.verify_mode = ssl.CERT_NONE

#黄金网站
url = "http://gold.jiagela.com/"

def url_open(url):
    try:
        hds=[{'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'},\
  {'User-Agent':'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.12 Safari/535.11'},\
  {'User-Agent':'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)'},\
  {'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:34.0) Gecko/20100101 Firefox/34.0'},\
  {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/44.0.2403.89 Chrome/44.0.2403.89 Safari/537.36'},\
  {'User-Agent':'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50'},\
  {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50'},\
  {'User-Agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0'},\
  {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1'},\
  {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1'},\
  {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11'},\
  {'User-Agent':'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11'},\
  {'User-Agent':'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11'}]
        res=requests.get(url,headers=hds[random.randint(0,len(hds)-1)],verify=False)
        html=res.content
        html_doc=str(html,'utf-8') 
        bs = BeautifulSoup(html_doc, 'html.parser')
        tables = bs.find_all("table")
        return (tables)
    except:
        pass
    
def get_today():
    h=datetime.datetime.now()
    today=h.strftime("%Y-%m-%d")    
    return today

def delete_files(directory):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if re.match('filesname.*\.ttt$', file): # 这里的正则表达式为删除所有后缀名为".txt"的文件
                path = os.path.join(root, file)
                os.remove(path)


def get_gold():
    try:
        data=url_open(url)
        return data
    except:
        return "无法获取到黄金价格信息"
def set_content():
    content = get_gold()
    t = time.localtime(time.time())
    today_data=get_today()
    # 调用函数并传入目标文件夹路径
    delete_files('/usr/local/nginx/html')
    with open('/usr/local/nginx/html/filesname'+today_data+'.ttt','w+') as file:
        print(str(content),2222333)
        file.write(str(content))
        file.close()



if __name__=="__main__":
    set_content()
    schedule.every().hour.do(set_content)

while True:
    schedule.run_pending()
    time.sleep(1)
